package com.itheima.service.impl;

import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Dept;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
@Autowired
    private EmpMapper empMapper;

@Autowired
    private DeptMapper deptMapper;

    /**
     * 查询全部部门
     * @return
     */
    @Override
    public List<Dept> list() {
    List<Dept> depts = deptMapper.selectlist();

        return depts;
    }

    /**
     * 删除一个部门
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeById(Integer id) {
        deptMapper.deleteById(id);

        empMapper.deleteByDeptId(id);
    }

    /**
     * 添加一个部门
     * @param dept
     */
    @Override
    public void insertById(Dept dept) {
        Integer count = deptMapper.selectCountByName(dept.getName());

        if(count > 0){
            throw new RuntimeException("部门已存在!");
        }
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertById(dept);

    }

    /**
     * 收集一个学生信息
     * @param id
     * @return
     */
    @Override
    public Dept selectOne(String id) {
        Dept dept = deptMapper.selectOne(id);
        return dept;
    }

    /**
     * 更新一个学生信息
     * 提问
     * @param dept
     */
    @Override
    public void updateOne(Dept dept) {
         dept.setUpdateTime(LocalDateTime.now());
         deptMapper.updateOne(dept);
    }


}
